package com.xbongbong.sys.model.impl;

import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.sys.domain.dao.ApiTokenDao;
import com.xbongbong.sys.domain.entity.ApiTokenEntity;
import com.xbongbong.sys.model.ApiTokenModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * apiToken业务
 * @author kaka
 * @version v1.0
 * @date 2019/8/29 16:46
 * @since v1.0
 */
@Service("apiTokenModel")
public class ApiTokenModelImpl implements ApiTokenModel {

    @Resource
    private ApiTokenDao apiTokenDao;
    @Resource
    private PaasRedisHelper paasRedisHelper;

    @Override
    public Integer insert(ApiTokenEntity entity) {
        return apiTokenDao.insert(entity);
    }

    @Override
    public Integer update(ApiTokenEntity entity) {
        return apiTokenDao.update(entity);
    }

    @Override
    public Integer save(ApiTokenEntity entity) {
        String key = entity.getCorpid() + "_" + entity.getOpenAppId();
        paasRedisHelper.removeValue(RedisPrefixConstant.API_TOKEN, key);
        if(entity.getId() == null || Objects.equals(0, entity.getId())) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public ApiTokenEntity getByCorpId(String corpid) {
        return apiTokenDao.getByCorpid(corpid, null);
    }

    @Override
    public ApiTokenEntity getByCorpId(String corpid, Integer openAppId) {
        return apiTokenDao.getByCorpid(corpid, openAppId);
    }

    @Override
    public List<ApiTokenEntity> getByCorpidAndExistOpenApp(String corpid) {
        return apiTokenDao.getByCorpidAndExistOpenApp(corpid);
    }

    @Override
    public List<String> getCorpidIn(Long endTimeStart) {
        return apiTokenDao.getCorpidIn(endTimeStart);
    }
}
